/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.trade.generator;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.payment.generator.AbstractGenerator;
import com.imoney.payment.member.enums.AddrType;
import com.imoney.payment.member.service.IAddrQueryService;
import com.imoney.payment.trade.domain.EmailItem;
import com.imoney.payment.trade.domain.Express;
import com.imoney.payment.trade.domain.TransferRequest;
import com.imoney.payment.trade.enums.BusinessType;
import com.imoney.payment.trade.enums.PortType;

/**
 * 类说明：<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-16		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-16
 */
@Service
public class ExpressOrderGenerator extends AbstractGenerator<Express> {

	@Resource
	private IAddrQueryService addrQueryService;

	@Override
	public Express generate(IRequest request) throws ServiceException {

		return null;
	}

	@Override
	public List<Express> generateList(IRequest request) throws ServiceException {
		TransferRequest trRequest = (TransferRequest) request;
		List<EmailItem> emailItems = trRequest.getEmailItems();

		long mailAddrId = 0; // 发货地址
		long recAddrId = 0; // 收货地址

		List<Express> expressList = new ArrayList<Express>();
		Express express = null;
		for (EmailItem item : emailItems) {
			if (!PortType.NONEED.equals(item.getPortFee())) {
				express = new Express();
				if (BusinessType.GATHER.equals(trRequest.getBusinessType())) {
					if (0 == mailAddrId) {
						mailAddrId = addrQueryService.queryAddrByNo(
								trRequest.getFromEmail(), AddrType.MAILADDR);
					}
					recAddrId = addrQueryService.queryAddrByNo(
							item.getToEmails(), AddrType.RECADDR);
					express.setdAddrId(recAddrId);
				}
				if (BusinessType.PAYMENT.equals(trRequest.getBusinessType())
						|| BusinessType.TRANSFER.equals(trRequest
								.getBusinessType())) {
					if (0 == mailAddrId) {
						mailAddrId = addrQueryService.queryAddrByNo(
								item.getToEmails(), AddrType.MAILADDR);
					}
					express.setdAddrId(item.getAddrId());
				}
				express.setjAddrId(mailAddrId);
				express.setTradeNo(item.getTradeNo());
				express.setPortType(item.getPortType());
				express.setPortFee("2".equals(item.getWhoPay()) ? 0 : item
						.getPortFee());
				express.setWhoPay(item.getPortFee() >= 1 ? "1" : item
						.getWhoPay());

				expressList.add(express);
			}
		}

		return expressList;
	}

}
